package com.ktg.mes.chanjet.task;


import com.ktg.common.exception.BussinessException;
import com.ktg.mes.chanjet.content.GetVoucherContent;
import com.ktg.mes.chanjet.response.GetVoucherResponse;
import com.ktg.mes.chanjet.spi.ChanjetSpi;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.service.IMdItemService;
import com.ktg.mes.wm.domain.WmItemRecpt;
import com.ktg.mes.wm.domain.WmItemRecptLine;
import com.ktg.mes.wm.service.IWmItemRecptLineService;
import com.ktg.mes.wm.service.IWmItemRecptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Component("purchasReceiveSycnTask")
public class PurchasReceiveSycnTask {

    @Autowired
    private ChanjetSpi chanjetSpi;

    @Autowired
    private IWmItemRecptService iWmItemRecptService;

    @Autowired
    private IWmItemRecptLineService iWmItemRecptLineService;


    @Autowired
    private IMdItemService iMdItemService;

    public void execute() {
        try {
            GetVoucherContent content = new GetVoucherContent();
            content.setPageSize(9999);
            content.setPageIndex(0);
            content.setParamDic(chanjetSpi.getDateParmDic());
            GetVoucherResponse response = chanjetSpi.findPurchaseReceiveList(content);
            Map<String, Object> data = response.getData();
            List<List<String>> datalist =(List<List<String>>)data.get("Rows");
            for (List<String> list : datalist) {
                long id = new BigDecimal(list.get(25)).longValue();
                WmItemRecpt wmItemRecpt = new WmItemRecpt();
                wmItemRecpt.setRecptId(id);
                wmItemRecpt.setRecptCode(list.get(0));
                SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
                wmItemRecpt.setRecptDate(sf.parse(list.get(1)));
                wmItemRecpt.setRecptName("");
                wmItemRecpt.setVendorCode(list.get(3));
                wmItemRecpt.setPoCode(list.get(6));
                wmItemRecpt.setVendorName(list.get(4));
                wmItemRecpt.setRemark(list.get(7));
                if(list.get(26) != null){
                    wmItemRecpt.setVendorId(new BigDecimal(list.get(26)).longValue());
                }
                wmItemRecpt.setVendorNick(list.get(5));
                wmItemRecpt.setWarehouseName(list.get(9));
                wmItemRecpt.setStatus(list.get(8));
                wmItemRecpt.setWarehouseCode(list.get(23));
                if(list.get(22) != null){
                    wmItemRecpt.setWarehouseId(new BigDecimal(list.get(22)).longValue());
                }
                wmItemRecpt.setUpdateTime(new Date());
                iWmItemRecptService.saveOrUpdate(wmItemRecpt);
                WmItemRecptLine wmItemRecptLine = new WmItemRecptLine();
                wmItemRecptLine.setLineId(id);
                wmItemRecptLine.setRecptCode(list.get(0));
                wmItemRecptLine.setRecptId(id);
                wmItemRecptLine.setItemName(list.get(10));
                wmItemRecptLine.setItemCode(list.get(11));
                wmItemRecptLine.setSpecification(list.get(12));
                wmItemRecptLine.setViscosity(list.get(13));
                wmItemRecptLine.setTag(list.get(14));
                wmItemRecpt.setRemark(list.get(7));
                wmItemRecptLine.setUnitOfMeasure(list.get(21));
                wmItemRecptLine.setPackingType(list.get(15));
                wmItemRecptLine.setQuantityRecived(new BigDecimal(list.get(18)));
                wmItemRecptLine.setBatchCode(list.get(19));
                wmItemRecptLine.setFromItemRecptId(wmItemRecpt.getRecptId());
                wmItemRecptLine.setWarehouseName(list.get(9));
                wmItemRecptLine.setMaterialsInDate(sf.parse(list.get(1)));
                wmItemRecptLine.setItemId(new BigDecimal(list.get(24)).longValue());
                wmItemRecptLine.setQuantityRecived(new BigDecimal(list.get(18)));
                wmItemRecptLine.setBatchCode(list.get(19));
                wmItemRecptLine.setInType("7");
                wmItemRecptLine.setWarehouseCode(list.get(23));
                if(list.get(22) != null){
                    wmItemRecptLine.setWarehouseId(new BigDecimal(list.get(22)).longValue());
                }
                wmItemRecptLine.setUpdateTime(new Date());
                iWmItemRecptLineService.saveOrUpdate(wmItemRecptLine);
            }
        } catch (Exception ex) {
            log.error("同步入库单 信息出错", ex);
            throw new BussinessException("执行失败");
        }
    }
}
